package cn.iocoder.yudao.module.forward.service.testsend;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.forward.controller.admin.testsend.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.testsend.TestSendDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.forward.dal.mysql.testsend.TestSendMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.forward.enums.ErrorCodeConstants.*;

/**
 * 演练发布 Service 实现类
 *
 * @author zhang
 */
@Service
@Validated
public class TestSendServiceImpl implements TestSendService {

    @Resource
    private TestSendMapper testSendMapper;

    @Override
    public Integer createTestSend(TestSendSaveReqVO createReqVO) {
        // 插入
        TestSendDO testSend = BeanUtils.toBean(createReqVO, TestSendDO.class);
        testSendMapper.insert(testSend);
        // 返回
        return testSend.getId();
    }

    @Override
    public void updateTestSend(TestSendSaveReqVO updateReqVO) {
        // 校验存在
        validateTestSendExists(updateReqVO.getId());
        // 更新
        TestSendDO updateObj = BeanUtils.toBean(updateReqVO, TestSendDO.class);
        testSendMapper.updateById(updateObj);
    }

    @Override
    public void deleteTestSend(Integer id) {
        // 校验存在
        validateTestSendExists(id);
        // 删除
        testSendMapper.deleteById(id);
    }

    private void validateTestSendExists(Integer id) {
        if (testSendMapper.selectById(id) == null) {
            throw exception(TEST_SEND_NOT_EXISTS);
        }
    }

    @Override
    public TestSendDO getTestSend(Integer id) {
        return testSendMapper.selectById(id);
    }

    @Override
    public PageResult<TestSendDO> getTestSendPage(TestSendPageReqVO pageReqVO) {
        return testSendMapper.selectPage(pageReqVO);
    }

}